<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      var order500 = function (orderType, pay, stack) {
        if (orderType === 1 && pay) {
          console.log("购买500定金，反100优惠券");
        } else {
          return "nextSuccessor";
        }
      };
      var order200 = function (orderType, pay, stack) {
        if (orderType === 2 && pay) {
          console.log("购买200定金，反50优惠券");
        } else {
          return "nextSuccessor";
        }
      };
      var orderNormal = function (orderType, pay, stack) {
        if (stack > 0) {
          console.log("无优惠购买产品");
        } else {
          console.log("没有库存啦");
        }
      };
      Function.prototype.after = function (fn) {
        var self = this;
        return function () {
          var ret = self.apply(this, arguments);
          if (ret === "nextSuccessor") {
            return fn.apply(this, arguments);
          }

          return ret;
        };
      };
      // order500.after(order200)
      /**
       * var self = order500
       * function() {
       *    var ret = self.apply(this,arguments)
       *    if(ret === "xxxx") {
       *        return order200(this.arguments)
       *    }
       *    return ret
       * }
      */
    
      var order = order500.after(order200).after(orderNormal);

      order(1, true, 500); // 输出：500元定金预购，得到100优惠券
      order(2, true, 500); // 输出：200元定金预购，得到50优惠券
      order(1, false, 500); // 输出：普通购买，无优惠券
    </script>
  </body>
</html>
